    
    // socket
    var io = require('socket.io').listen(8080);
    
    // require date.
    var dateFormat = require('dateformat');
    
    io.set(['polling', 'websocket']);
    
    var conn = require('mysql').createConnection({
        host:       'localhost',
        user:       'root',
        password:   '',
        database:   'chat'
    });
    conn.connect(function(err) {
        if(err) throw err;
    })
    
    var userList = {};
    
    io.sockets.on('connection', function(socket) {
        console.log('Connected ...');
        
        socket.emit('auth-visit', Object.keys(userList));
        
        /**
         * Login socket.
         */
        socket.on('auth-login', function(id) {
            conn.query('SELECT * from users where id = ?', [id], function(err, results) {
                if(err) throw err;
                else {
                    var user = results[0];
                    socket.emit('auth-logged', user);
                    
                    // Send to all clients.
                    socket.broadcast.emit('auth-joined',user.id);
                    user.socketID       = socket.id;
                    userList[user.id]   = user;
                    
                    // set Session.
                    socket.authInfo     = user;
                }
            });
        })
        
        /**
         * Logout socket.
         */
        socket.on('auth-logout', function() {
            var authInfo = socket.authInfo;
            
            if(authInfo != null) {
                delete userList[authInfo.id];
                socket.emit('auth-logout');
                socket.broadcast.emit('auth-leaved', authInfo.id);
            }
        });
        
        // Send to all
        socket.on('mess-send', function(data) {          
            var authInfo    = socket.authInfo;
            var now         = new Date();
            
            var created     = dateFormat(now, "yyyy-mm-dd, hh:mm:ss");
            
            var data        = {user_id: authInfo.id, message: data, created: created};
            
            conn.query('insert into room set ?', data, function(err, result) {
                if(err) throw err;
                else {
                    io.sockets.emit('mess-send-new', {authInfo: authInfo, mess: data});
                }
                
            });
        });
        
        /**
         * Send message to friend.
         */
        socket.on('friend-mess-send', function(data) {
            var authInfo    = socket.authInfo;
            var now         = new Date();
            var created     = dateFormat(now, "yyyy-mm-dd, hh:mm:ss");
            
            var dataInsert  = {message: data.message, from_user_id: authInfo.id, to_user_id: data.id, created: created, viewed: 0};
            
            conn.query('insert into chat set ?', dataInsert, function(err, results) {
                if(err) throw err;
                else {
                    //socket.broadcast.emit('friend-mess-send', data);
                    if(data.id in userList) {
                        var socketID        = userList[data.id].socketID;
                        var emitData        = authInfo;
                        emitData.messageId  = results.insertId;
                        emitData.message    = data.message;
                        
                        if (io.sockets.connected[socketID]) {
                            io.sockets.connected[socketID].emit('friend-mess', emitData);
                        }
                    }
                }
                
            });
        });
        
        socket.on('friend-mess-typing', function(id) {
            if(id in userList) {
                var socketID        = userList[id].socketID;
                var authInfo        = socket.authInfo;
                
                if (io.sockets.connected[socketID]) {
                    io.sockets.connected[socketID].emit('friend-mess-typing', authInfo.id);
                }
            }
        })
        
        /**
         * stop typing
         */
        socket.on('friend-typing-stop', function(id) {
            if(id in userList) {
                var socketID        = userList[id].socketID;
                var authInfo        = socket.authInfo;
                
                if (io.sockets.connected[socketID]) {
                    io.sockets.connected[socketID].emit('friend-typing-stop', authInfo.id);
                }
            }
        });
        
    });
    
    
    